package com.leetcode.题库;

/**
 * @author 覃国强
 * @date 2022-02-25 10:26
 */
public class 题库_0006_Z字形变换 {

  public static void main(String[] args) {
    Solution solution = new Solution();
    String s = "PAYPALISHIRING";
    int numRows = 3;
    String convert = solution.convert(s, numRows);
    System.out.println(convert);
  }

  static
  class Solution {
    public String convert(String s, int numRows) {
      if (numRows == 1) {
        return s;
      }

      StringBuilder[] builders = new StringBuilder[numRows];
      for (int k = 0; k < builders.length; ++k) {
        builders[k] = new StringBuilder();
      }
      char[] chars = s.toCharArray();
      int i = 0;
      int symbol = 1;
      for (int k = 0; k < chars.length; ++k, i += symbol) {
        builders[i].append(chars[k]);
        if (i == numRows - 1) {
          symbol = -1;
        } else if (i == 0) {
          symbol = 1;
        }
      }
      for (int k = 1; k < builders.length; ++k) {
        builders[0].append(builders[k]);
      }

      return builders[0].toString();
    }
  }

}
